/********** Copyright header - do not remove **********
 *
 * The Toadlet Engine
 *
 * Copyright 2009, Lightning Toads Productions, LLC
 *
 * Author(s): Alan Fischer, Andrew Fischer
 *
 * This file is part of The Toadlet Engine.
 *
 * The Toadlet Engine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * The Toadlet Engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with The Toadlet Engine.  If not, see <http://www.gnu.org/licenses/>.
 *
 ********** Copyright header - do not remove **********/

#include "TextureMaterialStreamer.h"

namespace toadlet{
namespace tadpole{

bool TextureMaterialStreamer::load(Stream::ptr stream,ResourceData *data,ResourceRequest *request){
	if(data==NULL){
		return false;
	}
	
	String extension=ResourceManager::findExtension(data->getName());
	if(extension!=(char*)NULL){
		ResourceStreamer *streamer=mEngine->getTextureManager()->getStreamer(extension);
		if(streamer==NULL){
			streamer=mEngine->getTextureManager()->getDefaultStreamer();
		}
		if(streamer!=NULL){
			TextureMaterialRequest::ptr materialRequest=new TextureMaterialRequest(mEngine,request);
			streamer->load(stream,NULL,materialRequest); // Pass in NULL instead of the MaterialData, since it's expecting a TextureData
			return true;
		}
	}
	
	return false;
}

void TextureMaterialStreamer::TextureMaterialRequest::resourceReady(Resource *resource){
	Texture::ptr texture=(Texture*)resource;
	Material::ptr material=mEngine->createDiffuseMaterial(texture);
	mRequest->resourceReady(material);
}

}
}
